<!DOCTYPE html>
<html>
<head>
  <title>ArtilleryGame!</title>

  <script type="text/javascript">
    ArtilleryGameModule = null;  // Global application object.
    statusText = 'NO-STATUS';
    var piGenerator = null;
    var paintInterval = null;

    // When the NaCl module has loaded, hook up an event listener to handle
    // messages coming from it via PPB_Messaging.PostMessage() (in C) or
    // pp::Instance.PostMessage() (in C++), and indicate success.
    function moduleDidLoad() {
      ArtilleryGameModule = document.getElementById('artillery_game');
      ArtilleryGameModule.addEventListener('message', handleMessage, false);
      ArtilleryGameModule.postMessage("paint");
      updateStatus('SUCCESS');
    }

    // The 'message' event handler.  This handler is fired when the NaCl module
    // posts a message to the browser by calling PPB_Messaging.PostMessage()
    // (in C) or pp::Instance.PostMessage() (in C++).  This implementation
    // simply displays the content of the message in an alert panel.
    function handleMessage(message_event) {
      document.forms.FireForm.Angle.value = message_event.data;
      //alert(message_event.data);
    }

    // If the page loads before the Native Client module loads, then set the
    // status message indicating that the module is still loading.  Otherwise,
    // do not change the status message.
    function pageDidLoad() {
      if (ArtilleryGameModule == null) {
        updateStatus('LOADING...');
      } else {
        // It's possible that the Native Client module onload event fired
        // before the page's onload event.  In this case, the status message
        // will reflect 'SUCCESS', but won't be displayed.  This call will
        // display the current message.
        updateStatus();
      }
    }

    // Set the global status message.  If the element with id 'statusField'
    // exists, then set its HTML to the status message as well.
    // opt_message The message test.  If this is null or undefined, then
    // attempt to set the element with id 'statusField' to the value of
    // |statusText|.
    function updateStatus(opt_message) {
      if (opt_message)
        statusText = opt_message;
      var statusField = document.getElementById('status_field');
      if (statusField) {
        statusField.innerHTML = statusText;
      }
    }
    function Fire() {
      var angle = document.forms.FireForm.Angle.value;
    	ArtilleryGameModule.postMessage("Angle: " + angle);
      //ArtilleryGameModule.postMessage("paint");
      return false; // cancel the default "GET" action when submitting a form
    }
    
  </script>
</head>
<body onload="pageDidLoad()">

<h1>Native Client Module ArtilleryGame</h1>
<p>

  <!-- Load the published .nexe.  This includes the 'nacl' attribute which
  shows how to load multi-architecture modules.  Each entry in the "nexes"
  object in the .nmf manifest file is a key-value pair: the key is the
  instruction set architecture ('x86-32', 'x86-64', etc.); the value is a URL
  for the desired NaCl module.
  To load the debug versions of your .nexes, set the 'nacl' attribute to the
  _dbg.nmf version of the manifest file.

  Note: Since this NaCl module does not use any real-estate in the browser,
  it's width and height are set to 0.

  Note: The <EMBED> element is wrapped inside a <DIV>, which has a 'load'
  event listener attached.  This wrapping method is used instead of attaching
  the 'load' event listener directly to the <EMBED> element to ensure that the
  listener is active before the NaCl module 'load' event fires.
  -->
  <div id="listener">
    <script type="text/javascript">
       document.getElementById('listener')
          .addEventListener('load', moduleDidLoad, true);
    </script>

    <embed name="nacl_module"
       id="artillery_game"
       width=320 height=240
       src="artillery_game.nmf"
       type="application/x-nacl" />
  </div>
</p>
<form name="FireForm"
      action=""
      method="get"
      onsubmit="return Fire()">
  Angle:
  <input type="text" id="Angle" name="Angle" value="45"/>
  <p/>
  <input type="submit" value="Fire" />
</form>
<h2>Status</h2>
<div id="status_field">NO-STATUS</div>
</body>
</html>
